package com.victor.bookretail.controller;

import cn.hutool.json.JSONUtil;
import com.victor.bookretail.common.ResultCode;
import com.victor.bookretail.domain.UserInfoBo;
import com.victor.bookretail.domain.UserTypeEnum;
import com.victor.bookretail.exception.BusinessException;
import com.victor.bookretail.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

@Api
@RestController
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    @ApiOperation(value = "Create user")
    @PostMapping("/user")
    public UserInfoBo createUser(@Valid @RequestBody UserInfoBo userInfoBo) {
        if(!UserTypeEnum.getAllUserTypeCodes().contains(userInfoBo.getType())) {
            log.warn("User type is invalid, user info:{}", JSONUtil.toJsonStr(userInfoBo));
            throw new BusinessException(ResultCode.PARAMETER_ERROR);
        }
        return userService.createUser(userInfoBo);
    }
}
